package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.syslog.tool.SysTraceLogWriter;
import com.augurit.gzcgwsp.sendNotice.entity.YwPublicOpinions;
import com.augurit.gzcgwsp.sendNotice.service.IFxWebService;
import com.augurit.gzcgwsp.sendNotice.service.IYwPublicOpinionsService;
import com.augurit.gzcgwsp.smartformCsdlwj.common.SmartDao;

@Service
@Transactional
public class FxWebServiceImpl implements IFxWebService{
	private static final Logger logger = Logger.getLogger(FxWebServiceImpl.class);
	@Autowired
	private SmartDao smartDao;
	@Autowired
	private IYwPublicOpinionsService ywPublicOpinionsService;
	/**
	 * 
	 * @param calls 调用方
	 * @param acceptNo 受理号
	 * @param opinion 评论内容
	 * @return true或者false
	 */
	public boolean opinions(String calls,String acceptNo,String opinion){
		YwPublicOpinions ywPublicOpinions = new YwPublicOpinions();
		ywPublicOpinions.setAcceptNo(acceptNo);
		ywPublicOpinions.setCalls(calls);
		ywPublicOpinions.setOpinion(opinion);
		if(StringUtils.isNotEmpty(opinion) && !"无".endsWith(opinion)){
			ywPublicOpinions.setIfAgreed("不满意");
		}else{
			ywPublicOpinions.setIfAgreed("满意");
		}
		
		ywPublicOpinions.setSubmitTime(new Date());
		System.out.println("123456");
		boolean success = ywPublicOpinionsService.saveOpinions(ywPublicOpinions);	
		return success;
	}
	
	/**
	 * 待办任务比对接口
	 * @param loginName
	 * @return xml
	 */
	public String getDaiBanInfo(String loginName) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("select jht.dbid_ from jbpm4_hist_task jht,jbpm4_hist_procinst jhp,");
			sb.append("wf_bus_instance wbi,wf_bus_template wbt,wf_bus_template_type wbtt ");
			sb.append(" where jht.procinst_ = jhp.id_ and jhp.id_ = wbi.proc_inst_id and ");
			sb.append("wbi.template_id = wbt.id and wbt.template_type_id = wbtt.id and jhp.state_ = 'active' ");
			sb.append(" and jht.assignee_ = :loginName and jht.state_ = 'open'");
			sb.append(" order by jht.create_ desc");
			SQLQuery query = smartDao.getSession().createSQLQuery(sb.toString());
			query.setString("loginName", loginName);
			List<BigDecimal> list = query.list();
			Document doc = DocumentHelper.createDocument();  
			Element root = doc.addElement("result");   
			Element ResponseStatus = root.addElement("ResponseStatus");
			Element retCode = ResponseStatus.addElement("retCode");
			retCode.addText("0");
			Element retMessage = ResponseStatus.addElement("retMessage");
			Element item = retMessage.addElement("item");
			if(list != null){
				for(int i=0;i<list.size();i++){
					item.addElement("id").addText(list.get(i).toString());
				}
			}
			return this.formatXML(doc,"UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("待办任务比对接口异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			Document doc = DocumentHelper.createDocument();  
			Element root = doc.addElement("result");   
			Element ResponseStatus = root.addElement("ResponseStatus");
			Element retCode = ResponseStatus.addElement("retCode");
			retCode.addText("1");
			Element retMessage = ResponseStatus.addElement("retMessage");
			retMessage.addText(e.getLocalizedMessage());
			return this.formatXML(doc,"UTF-8");
		}		
	}
	
	/**
	 * 格式化xml
	 * @param document
	 * @param charset
	 * @return
	 */
	public  String formatXML(Document document, String charset) {
        OutputFormat format = OutputFormat.createPrettyPrint(); 
        format.setEncoding(charset); 
        //format= OutputFormat.createCompactFormat();
        StringWriter sw = new StringWriter(); 
        XMLWriter xw = new XMLWriter(sw, format); 
        try { 
	        xw.write(document); 
	        xw.flush(); 
	        xw.close(); 
        } catch (IOException e) { 
        	e.printStackTrace();
        } 
        return sw.toString(); 
	}
}
